//
//  AllAlbumViewController.m
//  Everest
//
//  Created by Liu Chengbo on 14/12/29.
//  Copyright (c) 2014年 ThreeL. All rights reserved.
//

#import "AllAlbumViewController.h"
#import "UIColor+AddColor.h"
#import "NetHandler.h"
#import "FindHotAlbum.h"
#import "AlbumViewController.h"
#import "DownloadCell.h"
#import "ClassifyDetail.h"
#import "JingRoundView.h"
#import "AvReadioPlayer.h"
#define kCellWidth [UIApplication sharedApplication].keyWindow.bounds.size.width

@interface AllAlbumViewController () <UITableViewDataSource, UITableViewDelegate, JingRoundViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) JingRoundView *roundView;
@property (nonatomic, retain) NSMutableArray *arr;
@end

@implementation AllAlbumViewController
- (void)dealloc
{
    [_roundView release];
    [_arr release];
    [_mayBeLike release];
    [_tableView release];
    [super dealloc];
}

- (void)handleData
{
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/%@/1/30?device=iPhone", self.mayBeLike.uid];
    [NetHandler getDataWithUrl:str completion:^(NSData *data) {
        
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *tempArray = [result objectForKey:@"list"];
        
        self.arr = [NSMutableArray array];
        
        for (NSDictionary *dic in tempArray) {
            
            FindHotAlbum *hotAlbum = [[FindHotAlbum alloc] initWithDictionary:dic];
            
            [self.arr addObject:hotAlbum];
            
            [hotAlbum release];
            
        }
        [self.tableView reloadData];
        NSLog(@"打印数组: %@", self.arr);
    }];
}
- (void)viewWillAppear:(BOOL)animated
{
//    self.navigationController.navigationBarHidden = NO;
//    self.navigationItem.hidesBackButton = YES;
//    self.tabBarController.tabBar.hidden = YES;
//    self.navigationController.navigationBar.translucent = YES;
     //自定义tabbar
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 49, self.view.bounds.size.width, 49)];
    [self.view addSubview:backView];
    backView.backgroundColor = [UIColor tabBarColor];
    // 返回键
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [backView addSubview:backButton];
    backButton.frame = CGRectMake(10, 10, 30, 29);
    [backButton setImage:[UIImage imageNamed:@"iconfont-gerenfanhui.png"] forState:UIControlStateNormal];
    [backButton setTintColor:[UIColor sectionColor]];
    [backButton addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    // 播放键
    self.roundView = [JingRoundView defaultJingRoundView];
    self.roundView.frame = CGRectMake((self.view.bounds.size.width / 2) - 30, - 10, 60, 60);
    self.roundView.delegate = self;
    self.roundView.rotationDuration = 4.0;
    [backView addSubview:self.roundView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBarHidden = NO;
    //    self.navigationItem.hidesBackButton = YES;
//    self.tabBarController.tabBar.hidden = YES;
//    self.navigationController.navigationBar.translucent = YES;
    [self handleData];
    // navigation标题
//    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.title = @"全部专辑";
    self.navigationItem.hidesBackButton = YES;
    self.tabBarController.tabBar.hidden = YES;
//    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 15) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
//    UIView *naviView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
//    [self.view addSubview:naviView];
//    naviView.backgroundColor = [UIColor whiteColor];
//    [naviView release];
    [_tableView release];
    
}
// 自定义tabbar中播放按钮方法
-(void)playStatuUpdate:(BOOL)playState
{
    NSLog(@"%@...", playState ? @"播放": @"暂停了");
    if (playState == YES) {
        NSLog(@"播放");
        AvReadioPlayer *avPlayer = [AvReadioPlayer defaultAvPlayer];
        [avPlayer play];
    } else {
        NSLog(@"暂停");
        AvReadioPlayer *avPlayer = [AvReadioPlayer defaultAvPlayer];
        [avPlayer pause];
        
    }
}
- (void)backButtonClicked:(UIButton *)button
{
    NSLog(@"返回上一页");
    [self.navigationController popViewControllerAnimated:YES];
}
// section 高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
// section 个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arr.count;
}
// cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
// cell 背景透明
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor clearColor];
}
// cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *title = [[self.arr objectAtIndex:indexPath.section] title];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    
    CGRect rect = [title boundingRectWithSize:CGSizeMake(self.tableView.bounds.size.width / 2, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
//    CGFloat height1 = rect.size.height + self.view.bounds.size.height / 667 * 40;
    return rect.size.height + 50;
}
// 向cell中传值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"findHotReuse";
    DownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[DownloadCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
//    NSLog(@"%@", [self.arr objectAtIndex:indexPath.section]);
    cell.hotAlbum = [self.arr objectAtIndex:indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AlbumViewController *albumVC = [[AlbumViewController alloc] init];
    
    FindHotAlbum *find = [self.arr objectAtIndex:indexPath.section];
    
    ClassifyDetail *detail = [[ClassifyDetail alloc]init];
    detail.pageId = find.albumId;
    
    albumVC.classifyDetail = detail;
    
    [self.navigationController pushViewController:albumVC animated:YES];
    [detail release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
